package com.ss.android.account.activity;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Handler;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.webkit.CookieManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bytedance.common.utility.Logger;
import com.ss.android.account.Spipe;
import com.ss.android.account.model.SharePlatform;
import com.ss.android.article.browser.R;
import com.ss.android.common.app.WebViewTweaker;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.newmedia.activity.FragmentActivity;
import com.ss.android.newmedia.webview.DelegateWebViewClient;

public class AuthActivity extends FragmentActivity {
    ProgressBar a;
    Handler b;
    Runnable c;
    private WebView d;
    private View e;
    private CheckBox f;
    private Spipe g;
    private SharePlatform h;
    private TextView i;
    private boolean j;
    public AuthActivity() {

        this.h = null;
        this.j = false;
    }

    public final void a() {
        this.b.removeCallbacks(this.c);
        this.b.postDelayed(this.c, 500);
    }

    final boolean a(String arg4) {
        boolean v0_2;
        Logger.d("AuthActivity", "loading url: " + arg4);
        if (arg4.startsWith("snssdk")) {
            if (this.j) {
                try {
                    if (android.support.design.a.isTextEmpty(Uri.parse(arg4).getQueryParameter("session_key"))) {
                    } else if (this.h == null) {
                    } else if (this.h.mRecommendShowed) {
                    } else if (this.f.isChecked()) {
                        this.g.a(((Context) this), this.h);
                        MobClickCombiner.onEvent(((Context) this), "xiangping", "auth_recommend_on");
                    } else {
                        MobClickCombiner.onEvent(((Context) this), "xiangping", "auth_recommend_off");
                    }
                } catch (Exception v0) {
                }
            }
            Intent v0_1 = new Intent();
            v0_1.putExtra("callback", arg4);
            if (this.h != null) {
                v0_1.putExtra("platform", this.h.mName);
            }

            this.setResult(-1, v0_1);
            this.finish();
            v0_2 = true;
        } else {
            v0_2 = false;
        }

        return v0_2;
    }

    public int getLayout() {
        return R.layout.ia;
    }

    public void init() {
        super.init();
        this.b = new Handler();
        this.c = new com.ss.android.account.activity.b(this);
        String v3 = this.getIntent().getDataString();
        if (v3 != null && (v3.startsWith("http"))) {
            this.mTitleView.setText(com.ss.android.article.browser.R.string.ss_authorize_title);
            String v0 = null;
            try {
                v0 = Uri.parse(v3).getQueryParameter("platform");
                if (v0 != null) {
                    SharePlatform v1_1 = SharePlatform.getByName(v0);
                    if (v1_1 != null) {
                        this.mTitleView.setText(v1_1.mVerbose);
                    } else {
                    }
                } else {
                }
            } catch (Exception v1) {
            }

            this.g = Spipe.inst();
            if (v0 != null) {
                SharePlatform[] v4 = this.g.mSharePlatforms;
                int v5 = v4.length;
                int v1_2 = 0;
                while (v1_2 < v5) {
                    SharePlatform v6 = v4[v1_2];
                    if (v6.mName.equals(v0)) {
                        this.h = v6;
                    } else {
                        ++v1_2;
                        continue;
                    }

                    break;
                }
            }

            this.i = (TextView) this.findViewById(R.id.ss_recommend_auth);
            this.j = this.getResources().getBoolean(R.bool.r);
            this.e = this.findViewById(com.ss.android.article.browser.R.id.ss_bottom_bar);
            this.f = (CheckBox) this.findViewById(R.id.ss_checkbox);
            if ((this.j) && this.h != null && !this.h.mRecommendShowed) {
                this.e.setVisibility(View.VISIBLE);
                if (("sina_weibo".equals(this.h.mName)) || ("qq_weibo".equals(this.h.mName))) {
                    this.i.setText(com.ss.android.article.browser.R.string.ss_recommend_upon_auth_weibo);
                } else {
                    this.i.setText(com.ss.android.article.browser.R.string.ss_recommend_upon_auth_other);
                }

                if ("qzone_sns".equals(this.h.mName)) {
                    this.f.setChecked(false);
                } else {
                    this.f.setChecked(true);
                }
            }
            this.a = (ProgressBar) this.findViewById(R.id.ss_htmlprogessbar);
            CookieManager.getInstance().setAcceptCookie(true);
            this.d = (WebView) this.findViewById(R.id.ss_webview);
            WebSettings v0_1 = this.d.getSettings();
            v0_1.setJavaScriptEnabled(true);
            v0_1.setSupportZoom(true);
            v0_1.setSupportMultipleWindows(false);
            v0_1.setBuiltInZoomControls(true);
            this.d.setWebViewClient(new b(this));
            this.d.setWebChromeClient(new a(this));
            com.ss.android.account.bb.a().a(v3, this.d);
            Logger.d("Spipe_Auth", "url: " + v3);
        } else {
            this.finish();
        }
    }

    public void onDestroy() {
        WebViewTweaker.clearWebviewOnDestroy(this.d);
        super.onDestroy();
    }

    public void onPause() {
        super.onPause();
        WebViewTweaker.tweakPauseIfFinishing(((Context) this), this.d);
    }

    final class a extends WebChromeClient {
        private final AuthActivity a;

        a(AuthActivity arg1) {
            this.a = arg1;

        }

        public final void onProgressChanged(WebView arg4, int arg5) {
            AuthActivity v0 = this.a;
            v0.a.setProgress(arg5);
            v0.b.removeCallbacks(v0.c);
            if (v0.a.getVisibility() != View.VISIBLE) {
                v0.a.startAnimation(AnimationUtils.loadAnimation(v0, 17432576));
                v0.a.setVisibility(View.VISIBLE);
            }

            if (arg5 >= 100) {
                this.a.a();
            }
        }
    }

    final class b extends DelegateWebViewClient {
        private final AuthActivity a;

        b(AuthActivity arg1) {
            this.a = arg1;

        }

        public final void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
            super.onReceivedError(webView, errorCode, description, failingUrl);
            this.a.a();
        }

        @TargetApi(value = 8)
        public final void onReceivedSslError(WebView arg4, SslErrorHandler arg5, SslError arg6) {
            Logger.w("AuthActivity", "ssl error: " + arg6);
            arg5.proceed();
        }

        public final boolean shouldOverrideUrlLoading(WebView arg2, String arg3) {
            return this.a.a(arg3);
        }
    }
}

